#include "mainwindow.h"
//#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget *central=new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(central);
    // 创建 tab widget，并允许关闭
    tabWidget = new QTabWidget(this);
    tabWidget->setTabsClosable(true);
    connect(tabWidget, &QTabWidget::tabCloseRequested, this, &MainWindow::closeTab);

    // 添加一个按钮来添加标签页
    addTabButton = new QPushButton("添加新标签页", this);
    connect(addTabButton, &QPushButton::clicked, this, &MainWindow::addNewTab);

    layout->addWidget(addTabButton);
    layout->addWidget(tabWidget);

    setCentralWidget(central);

    // 初始添加一个标签页
    addNewTab();
    //ui->setupUi(this);
}

MainWindow::~MainWindow() {}

void MainWindow::addNewTab() {
    QTextEdit *editor = new QTextEdit(this);
    tabWidget->addTab(editor, QString("标签 %1").arg(tabCount++));
    tabWidget->setCurrentWidget(editor);
}

void MainWindow::closeTab(int index) {
    QWidget *tab = tabWidget->widget(index);
    tabWidget->removeTab(index);
    delete tab;
}
